/*
 * addServiceType.java
 *
 * Created on February 28, 2006, 8:13 AM
 */

package phs_project;

import java.awt.event.KeyEvent;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.xml.transform.Result;
/**
 *
 * @author  Administrator
 */
public class addServiceType extends javax.swing.JDialog {
    
    /** Creates new form addServiceType */
    public addServiceType(JFrame parent,boolean modal ) {
        super(parent,modal);
        initComponents();
        //SetColHeader();       
        sqlTable();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jTextField3 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        txtNameType = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        btUpdate = new javax.swing.JButton();
        btAdd = new javax.swing.JButton();
        btDel = new javax.swing.JButton();
        btExit = new javax.swing.JButton();

        jTextField3.setText("jTextField3");
        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Th\u00eam lo\u1ea1i d\u1ecbch v\u1ee5");
        setAlwaysOnTop(true);
        setResizable(false);
        jLabel1.setText("Lo\u1ea1i d\u1ecbch v\u1ee5");

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addContainerGap(17, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap(17, Short.MAX_VALUE)
                .add(jLabel1))
        );

        txtNameType.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                addKeyEvent(evt);
                CancelKeyEvent(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap(95, Short.MAX_VALUE)
                .add(txtNameType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 192, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .add(txtNameType, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh s\u00e1ch lo\u1ea1i d\u1ecbch v\u1ee5"));
        model = new javax.swing.table.DefaultTableModel();
        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Số TT", "Tên"
            }
        ));
        table.setModel(model);
        table.setFocusable(false);// Do this only if there's no need for the component to have the focus
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(table);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 158, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        btUpdate.setMnemonic('U');
        btUpdate.setText("S\u1eeda");
        btUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btUpdateActionPerformed(evt);
            }
        });

        btAdd.setMnemonic('A');
        btAdd.setText("Th\u00eam");
        btAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddActionPerformed(evt);
            }
        });

        btDel.setMnemonic('D');
        btDel.setText("X\u00f3a");
        btDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDelActionPerformed(evt);
            }
        });

        btExit.setMnemonic('E');
        btExit.setText("Tho\u00e1t");
        btExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExitActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                .add(btAdd, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                .add(29, 29, 29)
                .add(btUpdate)
                .add(31, 31, 31)
                .add(btDel)
                .add(30, 30, 30)
                .add(btExit, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 67, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btUpdate)
                    .add(btExit)
                    .add(btDel)
                    .add(btAdd))
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-394)/2, (screenSize.height-369)/2, 394, 369);
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed

    private void CancelKeyEvent(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_CancelKeyEvent
// TODO add your handling code here:
        if(evt.getKeyCode() == KeyEvent.VK_ESCAPE){
            this.dispose();
        }
    }//GEN-LAST:event_CancelKeyEvent

    private void addKeyEvent(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_addKeyEvent
// TODO add your handling code here:
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            btAdd.doClick();
        }
    }//GEN-LAST:event_addKeyEvent

    private void btUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btUpdateActionPerformed
// TODO add your handling code here:
    updateServiceType();
    sqlTable();
    }//GEN-LAST:event_btUpdateActionPerformed

    private void btDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDelActionPerformed
// TODO add your handling code here:
        String typeName = txtNameType.getText();
        delServicesType(typeName);
        sqlTable();
        
    }//GEN-LAST:event_btDelActionPerformed

    private void tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseClicked
// TODO add your handling code here:
    int selectedRow = table.getSelectedRow();
    txtNameType.setText(((String)table.getValueAt(selectedRow,0)));
    }//GEN-LAST:event_tableMouseClicked

    private void btExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExitActionPerformed
// TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btExitActionPerformed

    private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
// TODO add your handling code here:
        String typeName = new validate().getString(txtNameType.getText());
        if(typeName!= null){
           addServiceType(typeName);
           sqlTable();
           txtNameType.setText("");
           txtNameType.requestFocus();
       }
        else{
            JOptionPane.showMessageDialog(this,"Type name is not space");
        }
    }//GEN-LAST:event_btAddActionPerformed
    
    /**
     * @param args the command line arguments
     */

 public static void main(String args[]) {
       // java.awt.EventQueue.invokeLater(new Runnable() {
        //    public void run() {
         //       new addServiceType().setVisible(true);
         //   }
       // });
    }
    
    //User method 
private void sqlTable(){
    try{
        Connection con = new connectDatabase().getConnection();
        Statement st = con.createStatement();
        String sql = "SELECT typeName AS 'Loại dịch vụ' FROM servicesType";
        ResultSet rs = st.executeQuery(sql);
        displayResult(rs,table);
        con.close();
        st.close();
        rs.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
}
 private void displayResult(ResultSet rs,JTable table) {
        ResultsModel model = new ResultsModel();     // Create a table model
        model.setResultSet(rs);      
        table.setModel(model); 
}
    
private void addServiceType(String typename){
    //insert into orders
    try	{
            Connection con = new connectDatabase().getConnection();
            Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            String sql = "INSERT INTO servicesType (TypeName) VALUES(N'"+typename+"')";
            st.executeUpdate(sql);
            con.close();
            st.close();
	}
    catch(SQLException se){
	se.printStackTrace();
    }
}

private void updateServiceType(){
    int row;
    String name,typename,sql;
    row = table.getSelectedRow();
    typename = new validate().getString(txtNameType.getText());
    if((row!=-1)&&(typename!=null)){      
        try{
            Connection con = new connectDatabase().getConnection();
            Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            name = (String)table.getValueAt(row,0);
            sql = "UPDATE servicesType SET TypeName =N'"+typename+"'";
            sql = sql + " WHERE TypeName = N'"+name+"'";            
            int Result = st.executeUpdate(sql);
            if(Result >= 0 ){
                javax.swing.JOptionPane.showMessageDialog(this,"Recod serive update");
                txtNameType.setText("");             
            con.close();
            st.close();            
            }
        }
        catch(SQLException ce){
            System.out.println(ce);
        }
    }
    if(row == -1){
         javax.swing.JOptionPane.showMessageDialog(this,"Please select service name!");
    }
}

private  void delServicesType(String typename){
   int row;
   row = table.getSelectedRow();
   if(row != -1){
        try{
            Connection con = new connectDatabase().getConnection();
            Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            String sql = "DELETE servicesType WHERE TypeName = N'"+typename+"'";
            st.execute(sql);
            txtNameType.setText("");
            con.close();
            st.close();
        }
        catch(SQLException e){
            e.printStackTrace();
        }
   }
   else{
       javax.swing.JOptionPane.showMessageDialog(this,"Please select service type name!");
   }
}

    public void view(){
        try {
  Connection con = new connectDatabase().getConnection();
            Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            String sql = "DELETE servicesType WHERE TypeName = 'Sao lại thế'";
            st.execute(sql);
            txtNameType.setText("");
            con.close();
            st.close();
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }
    //Value user
  
    private javax.swing.table.DefaultTableModel model; 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdd;
    private javax.swing.JButton btDel;
    private javax.swing.JButton btExit;
    private javax.swing.JButton btUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTable table;
    private javax.swing.JTextField txtNameType;
    // End of variables declaration//GEN-END:variables
    
}
